<template>
    <div class="calender">
        <div class="top">
            <!-- <div class="preDate"> -->
            <!-- <button class="preYear" @click="getMyDate(year-parseInt(1,10),month)">&lt;&lt;</button> -->

            <!-- <button class="preMonth" >&lt;</button> -->
            <!-- </div> -->
            <div class="data_riqi">
                <img src="@/assets/arrow-left-filling.png" alt="" @click="getMyDate(year,month-parseInt(1,10))" class="left_but">
                <span class="riqi">{{year}}年{{month}}月</span>
                <img class="right_but" src="@/assets/arrow-right-filling.png" alt="" @click="getMyDate(year,month+parseInt(1,10))">
            </div>
            <!-- <div class="nextDate"> -->

            <!-- <button class="nextMonth" >&gt;</button> -->
            <!-- <button class="nextYear" @click="getMyDate(year+parseInt(1,10),month)">&gt;&gt;</button> -->
            <!-- </div> -->
        </div>

        <div class="bottom">
            <table>
                <thead>
                    <tr>
                        <th v-for="(item,index) in weeks" :key="index">
                            {{item.name}}
                        </th>
                    </tr>
                </thead>
                <tbody>
                    <tr v-for="(item,index) in all" :key="index">
                        <th v-for="(item2,index) in item" :key="index" v-if="item2===day">{{item2}}</th>
                        <th v-else-if="item2>0">{{item2}}</th>
                        <th v-else></th>
                    </tr>
                </tbody>
            </table>
        </div>
    </div>
</template>

<script>
export default {
  name: 'Calender',
  props: {
    signDates: {
      type: Array,
      default() {
        return []
      }
    }
  },
  watch: {
    signDates(val) {
      console.log(val, 7)
    }
  },
  data() {
    return {
      year: 2019, // 设置当前年份
      month: 8, // 设置当前月份
      day: 1, // 设置当前号数
      all: [], // 日历表格数组
      weeks: [
        { id: 0, name: '日' },
        { id: 1, name: '一' },
        { id: 2, name: '二' },
        { id: 3, name: '三' },
        { id: 4, name: '四' },
        { id: 5, name: '五' },
        { id: 6, name: '六' }
      ]
    }
  },
  beforeMount() {
    this.getMyDate()
  },
  methods: {
    getMyDate(year = false, month = false) {
      let obj // 初始化的时间对象
      const first = [] // 第1排的数据
      const second = [] // 第2-6排的数据
      let firstDay // 判断当月第一天是周几，作为日期对象的开头
      if (year && month) {
        // 处理月份越界问题
        if (month < 1) {
          this.year -= 1
          this.month = 12
        } else if (month > 12) {
          this.year += 1
          this.month = 1
        } else {
          this.month = month
          this.year = year
        }
        obj = new Date(this.year, month, this.day)
        this.all = [] // 每次请求时把日期数组清空
      } else {
        obj = new Date()
        this.year = obj.getFullYear() // 查询年份
        this.month = obj.getMonth() + 1 // 查询月份
        this.day = obj.getDate() // 查询多少号
      }

      const s = this.year + '/' + this.month + '/' + 1
      firstDay = new Date(s).getDay() // 查询当月的第一天是周几
      const days = new Date(this.year, this.month, 0).getDate() // 判断当前月份有多少天

      // 生成日历表格
      // 生成第一排的数据
      let index
      const data1 = [7, 1, 2, 3, 4, 5, 6]
      data1.forEach((i, item) => {
        if (item === firstDay) {
          index = i
        }
      })
      for (let i = 0; i < 7; i++) {
        if (i < index) {
          first.push(0)
        } else {
          first.push(i - index + 1)
        }
      }

      // 生成第2-6排数据
      const index2 = first[first.length - 1]
      for (let i = 0; i < 5; i++) {
        const d = []
        for (let j = 1; j < 8; j++) {
          if (i * 7 + index2 + j > days) {
            d.push(0)
          } else {
            d.push(i * 7 + index2 + j)
          }
        }
        second.push(d)
      }
      // 将第1排和第2-6排数据合到一起
      this.all.push(first)
      second.forEach(x => this.all.push(x))
      const flag = this.all[0].every(item => {
        return item == 0
      })
      console.log(flag)
      if (flag) {
        this.all.splice(0, 1)
      }
      console.log(this.all, 98)
    }
  },
  created() {
    console.log(this.signDates, 777)
  }
}
</script>

<style lang="scss" scoped>
.data_riqi
{
    margin-left: 20px;
    margin-top: 15px;
    display: flex;
    align-items: center;

    .riqi{
        margin: 0 10px;
        font-size: 18px;
    }
}
.left_but,
.right_but {
  width: 14px;
  height: 14px;
}
.calender {
  width: 100%;
  height: 100%;
  box-shadow: 0 0 1px #9e9c9c;
  display: flex;
  flex-flow: column;
  margin-left: 0px;
  background-color: white;
  border-radius: 20px;
  overflow: hidden;
  box-shadow: 0px 1px 10px 0px #ccc;
}
.top {
  width: 700px;
  line-height: 40px;
  color: #696868;
  display: flex;
  align-items: center;
  justify-content: flex-start;
}
.bottom {
  flex: 7;
}
table {
  width: 100%;
  height: 100%;
}
/deep/th {
  color: #595959;
  transition: all 0.5s;
  border-radius: 50%;
  height: 40px;
  width: 35px;
  font-size: 16px;
  font-weight: 100;
}
tbody > tr > th:hover {
  background-color: #e5e6e6;
}
.active,
.active:hover {
  background-color: #fb4023;
  color: white;
  width: 35px;
  height: 35px;
}
.top > div:nth-child(2) {
  width: 100px;
  height: 100%;
  margin: 0 auto;
}
.preDate,
.nextDate {
  display: flex;
}
.preYear,
.nextYear {
  flex: 2;
}
.preMonth,
.nextMonth {
  flex: 1;
}
.preDate > button,
.nextDate > button {
  border: none;
  background-color: white;
  font-size: 17px;
  color: #999999;
  transition: color 0.8s;
  font-weight: 100;
  outline: none;
}
.preMonth {
  text-align: left;
}
.nextMonth {
  text-align: right;
}
.preMonth:hover,
.nextMonth:hover,
.preYear:hover,
.nextYear:hover {
  color: red;
  cursor: pointer;
}
</style>
